//
//  WH_LabelView.m
//  App_6.23
//
//  Created by wanghao on 16/7/28.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_LabelView.h"
#import "WH_Home.h"

@interface WH_LabelView ()<UIScrollViewDelegate>


@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIScrollView *bigScroll;
@property (nonatomic, strong) NSTimer *myTimer;
@property (nonatomic, assign) NSInteger index;


@end
@implementation WH_LabelView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        

        
    }
    return self;
}
- (void)createSubViews:(NSMutableArray *)images{
    
    
    if (images.count != 0){
        
        
        self.imageArray = images;
        id imageFirst = [self.imageArray firstObject];
        id imageLast = [self.imageArray lastObject];
        
        [self.imageArray insertObject:imageLast atIndex:0];
        [self.imageArray addObject:imageFirst];
        
        
        self.bigScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        self.bigScroll.tag = 2017;
        self.bigScroll.pagingEnabled = YES;
        self.bigScroll.bounces = NO;
        self.bigScroll.showsVerticalScrollIndicator = NO;
        self.bigScroll.delegate = self;
        self.bigScroll.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height*self.imageArray.count);
        self.bigScroll.contentOffset = CGPointMake(0, self.bounds.size.height);
        //轮播图
        for (int i = 0; i < self.imageArray.count; i++) {
            
            WH_Home *name = self.imageArray[i];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.size.height*i, self.bounds.size.width, self.bounds.size.height)];
            //第三方图片赋值
            label.text = [NSString stringWithFormat:@"恭喜%@夺宝%@",name.user_nick,name.product_name];
            label.font = [UIFont systemFontOfSize:14.f];
            label.textColor = [UIColor lightGrayColor];
            [WH_AppTools changeColor:label.text color:[UIColor blueColor] xx:name.user_nick nn:label];
            [self.bigScroll addSubview:label];
            label.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [label addGestureRecognizer:tap];
        }
        [self addSubview:self.bigScroll];

        self.myTimer = [NSTimer scheduledTimerWithTimeInterval:3.5 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }
    //[self getfocusImagesView];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    UILabel *label = (UILabel *)tap.view;
    
    NSInteger number = (NSInteger)label.frame.origin.y / (NSInteger)label.frame.size.height;
    WH_Home *neST = self.imageArray[number];
    
    self.myBlock(neST);
}

-(void)block:(AppToolsBlock)block{
    
    self.myBlock = block;
}
-(void)timerAction:(NSTimer *)timer
{
    
    //使用scrollview自带的滚动动画
    [UIView animateWithDuration:.5 animations:^{
        
        [self.bigScroll setContentOffset:CGPointMake(0, (self.index + 1) * self.bigScroll.frame.size.height)];
        
    }];
    self.index++;
    //如果滑动到最后一页
    if (self.bigScroll.contentOffset.y == (self.imageArray.count-1)*self.bigScroll.frame.size.height) {
        //快速移动到第一页
        [self.bigScroll setContentOffset:CGPointMake(0, self.bigScroll.frame.size.height)];
        
        self.index = 0;
        
    }
    
}

//定时器在不断循环滚动,就不会减速停止,此方法就不会走
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (self.bigScroll.contentOffset.y >= self.bigScroll.frame.size.height * (self.imageArray.count - 1)) {
        
        NSLog(@"最后一页");//循环滚动
        [self.bigScroll setContentOffset:CGPointMake(0, self.bigScroll.frame.size.height) animated:NO];
    }
    
    if (self.bigScroll.contentOffset.y <= 0) {
        
        [self.bigScroll setContentOffset:CGPointMake(0, self.bigScroll.frame.size.height * (self.imageArray.count - 2)) animated:NO];
    }
    
    //不管怎么移动,保证self.index存的是当前的页码
    self.index = self.bigScroll.contentOffset.y / self.bigScroll.frame.size.height -1;
    
}
//当手指接触的时候让定时器停止
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

}
//当手指离开的时候开始滑动
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
